<template>
  <a-card :bordered='false'>

    <!-- 查询区域 -->
    <div class='table-page-search-wrapper'>
      <a-form layout='inline' @keyup.enter.native='searchQuery'>
        <a-row :gutter='24'>

          <a-col :md='6' :sm='8'>
            <a-form-item label='商品名称'>
              <!--<a-input placeholder="请输入商品名称" v-model="queryParam.name"></a-input>-->
              <j-input placeholder='请输入商品名称' v-model='queryParam.name'></j-input>
            </a-form-item>
          </a-col>
          <a-col :md='6' :sm='8'>
            <a-form-item label='货号'>
              <!--<a-input placeholder="请输入货号" v-model="queryParam.productSn"></a-input>-->
              <j-input placeholder='请输入货号' v-model='queryParam.productSn'></j-input>

            </a-form-item>
          </a-col>
          <template v-if='toggleSearchStatus'>
            <a-col :md='6' :sm='8'>
              <a-form-item label='商品类型'>
<!--                <j-dict-select-tag v-model='queryParam.productCategoryId' placeholder='请选择商品分类'
                                   dictCode="pms_product_category,name,id,show_status='1' order by sort" />-->
                <a-select
                  v-model="queryParam.productCategoryId"
                  style="width: 100%"
                  :getPopupContainer="node => node.parentNode"
                  placeholder="请选择商品类型">
                  <a-select-option v-for="(category,categoryIndex) in categoryList" :key="categoryIndex.toString()"
                                   :value="category.id">
                    {{ category.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md='6' :sm='8'>
              <a-form-item label='线上销售'>
                <j-dict-select-tag v-model='queryParam.publishStatus' placeholder='请选择线上销售'
                                   dictCode="yn" />
              </a-form-item>
            </a-col>
          </template>
          <a-col :md='6' :sm='8'>
            <span style='float: left;overflow: hidden;' class='table-page-search-submitButtons'>
              <a-button type='primary' @click='searchQuery' icon='search'>查询</a-button>
              <a-button @click='searchReset' icon='reload' style='margin-left: 8px'>重置</a-button>
              <a @click='handleToggleSearch' style='margin-left: 8px'>
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button @click="handleProduct" type="primary" icon="block">商品库</a-button>
      <a-button @click="recycleBinVisible=true" icon="hdd">回收站</a-button>
      <a-button type="primary" icon="download" @click="handleExportModal">导出</a-button>
      <!--<a-button type="primary" icon="download" @click="handleExportXls('商品信息')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>-->
      <!--<a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>-->
    </div>

    <!-- table区域-begin -->
    <div>
      <div class='ant-alert ant-alert-info' style='margin-bottom: 16px;'>
        <i class='anticon anticon-info-circle ant-alert-icon'></i> 已选择 <a
        style='font-weight: 600'>{{ selectedRowKeys.length }}</a>项
        <a style='margin-left: 24px' @click='onClearSelected'>清空</a>
      </div>

      <a-table
        ref='table'
        size='middle'
        bordered
        rowKey='id'
        :columns='columns'
        :dataSource='dataSource'
        :pagination='ipagination'
        :loading='loading'
        :rowSelection='{selectedRowKeys: selectedRowKeys, onChange: onSelectChange,type:type}'
        @change='handleTableChange'
        :customRow='clickThenCheck'>

        <span slot='eStatus' slot-scope='text, record'>
          <a-switch size='small' :checked="(text === '1')" @change='handlePublishStatus(record)' />
        </span>

        <span slot='wStatus' slot-scope='text, record'>
          <a-switch size='small' :checked='(text === 1)' />
        </span>

        <span slot='stStatus' slot-scope='text, record'>
          <a-switch size='small' :checked="(text === 1)" @change='handlePublishStatus2(record)' />
        </span>

        <span slot='action' slot-scope='text, record'>
          <a @click='handleEdit(record)'>编辑</a>
          <a-divider type='vertical' />
          <a @click='handleCopy(record)'>拷贝</a>
          <a-divider type='vertical' />
          <a-dropdown>
            <a class='ant-dropdown-link'>更多 <a-icon type='down' /></a>
            <a-menu slot='overlay'>
              <a-menu-item v-has="'product:wx'">
                <a href='javascript:;' @click='handleSyncWxMa(record.id)'>同步到微信</a>
              </a-menu-item>
              <a-menu-item v-has="'product:wx'">
                <a href='javascript:;' @click='handleGetWxMa(record.id)'>查看微信商品信息</a>
              </a-menu-item>
              <a-menu-item>
                <a href='javascript:;' @click='handleStatusDetail(record)'>商品标签</a>
              </a-menu-item>
              <a-menu-item>
                <a href='javascript:;' @click='handlePicDetail(record)'>商品主图</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title='确定删除吗?' @confirm='() => handleDelete(record.id)'>
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>
    <!-- table区域-end -->

    <a-tabs defaultActiveKey='1'>
      <a-tab-pane tab='SKU' :key='refKeys[0]' :forceRender='true'>
        <pmsSkuStock-List ref='PmsSkuStockList'></pmsSkuStock-List>
      </a-tab-pane>
      <a-tab-pane tab='图片' :key='refKeys[1]' :forceRender='true'>
        <pms-product-pics-list ref='PmsProductPicsList'></pms-product-pics-list>
      </a-tab-pane>
      <a-tab-pane tab='认证' :key='refKeys[2]' :forceRender='true'>
        <pms-product-qualification-pics-list
          ref='PmsProductQualificationPicsList'></pms-product-qualification-pics-list>
      </a-tab-pane>
    </a-tabs>

    <!-- 表单区域 -->
    <pmsProduct-modal ref='modalForm' @ok='modalFormOk'></pmsProduct-modal>
    <select-partner-product-modal ref="productModalForm" @selectFinished="selectOK"/>
    <!-- 状态修改快捷方式 -->
    <pms-product-status-modal ref='productStatusModal' @ok='modalFormOk'></pms-product-status-modal>
    <!-- 主图修改快捷方式 -->
    <pms-product-pic-modal ref='productPicModal' @ok='modalFormOk'></pms-product-pic-modal>

    <pms-product-export-modal ref='productExportModal' @ok='modalFormOk'></pms-product-export-modal>

    <pms-product-recycle-bin-modal :visible.sync='recycleBinVisible' @ok='modalFormOk' />
  </a-card>
</template>

<script>
  import PmsProductModal from './modules/PmsProductModal'
  import PmsProductStatusModal from './modules/PmsProductStatusModal'
  import PmsProductPicModal from './modules/PmsProductPicModal'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import { httpAction, deleteAction, postAction, getAction } from '@/api/manage'
  import PmsSkuStockList from './PmsSkuStockList'
  import PmsSkuStockModal from './modules/PmsSkuStockModal'
  import PmsProductPicsList from './PmsProductPicsList'
  import JInput from '@/components/jeecg/JInput'
  import PmsProductRecycleBinModal from './modules/PmsProductRecycleBinModal'
  import PmsProductQualificationPicsList from './PmsProductQualificationPicsList'
  import SelectPartnerProductModal from './modules/SelectPartnerProductModal'
  import PmsProductExportModal from '@views/pms/modules/PmsProductExportModal'

export default {
  name: 'PmsProductList',
  mixins: [JeecgListMixin],
  components: {
    PmsProductModal,
    PmsProductStatusModal,
    PmsProductPicModal,
    PmsSkuStockModal,
    PmsSkuStockList,
    PmsProductPicsList,
    JInput,
    PmsProductRecycleBinModal,
    PmsProductQualificationPicsList,
    SelectPartnerProductModal,
    PmsProductExportModal
  },
  data() {
    return {
      refKeys: ['PmsSkuStockList', 'PmsProductPicsList', 'PmsProductQualificationPicsList'],
      description: '商品信息管理页面',
      recycleBinVisible: false,
      categoryList: [],
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '品牌',
          align: 'center',
          dataIndex: 'brandName'
        },
        {
          title: '分类',
          align: 'center',
          dataIndex: 'productCategoryName'
        },
        {
          title: '商品名称',
          align: 'center',
          dataIndex: 'name'
        },
        {
          title: '线上销售',
          align: 'center',
          dataIndex: 'publishStatus',
          scopedSlots: { customRender: 'eStatus' }
        },
        {
          title: '门店销售',
          align: 'center',
          dataIndex: 'storeStatus',
          scopedSlots: { customRender: 'stStatus' }
        },
        {
          title: '微信交易组件',
          align: 'center',
          dataIndex: 'wxStatus',
          scopedSlots: { customRender: 'wStatus' }
        },
        {
          title: '货号',
          align: 'center',
          dataIndex: 'productSn'
        },
        /*{
          title: '价格',
          align:"center",
          dataIndex: 'price'
        },
        {
          title: '促销价格',
          align:"center",
          dataIndex: 'promotionPrice'
        },*/
        {
          title: '销量',
          align: 'center',
          dataIndex: 'sale'
        },
        {
          title: '收藏',
          align: 'center',
          dataIndex: 'totalCount'
        },
        {
          title: '排序',
          align: 'center',
          dataIndex: 'sort'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          width: 160,
          scopedSlots: { customRender: 'action' }
        }
      ],
      type: 'radio',
      url: {
        addBatch: "/pms/pmsProduct/addBatch",
        edit: '/pms/pmsProduct/edit',
        editProduct: '/pms/pmsProduct/editProductStatus',
        list: '/pms/pmsProduct/list',
        delete: '/pms/pmsProduct/delete',
        deleteBatch: '/pms/pmsProduct/deleteBatch',
        wxmaSyncBatch: '/pms/pmsProduct/wxmaSyncBatch',
        wxmaGetProduct: '/pms/pmsProduct/wxmaGetProduct',
        exportXlsUrl: 'pms/pmsProduct/exportXls',
        importExcelUrl: 'pms/pmsProduct/importExcel',
        getProductCategorys: '/pms/pmsProductCategory/getProductCategorys',
      }
    }
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  created() {
      this.getCategoryList()
  },
  methods: {
    getCategoryList() {
        getAction(this.url.getProductCategorys, {}).then(res => {
          if (res.success) {
            this.categoryList = res.result
          } else {
            console.log(res.message)
          }
        })
      },
    clickThenCheck(record) {
      return {
        on: {
          click: () => {
            this.onSelectChange(('' + record.id).split(','), [record])
          }
        }
      }
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectionRows = selectionRows
      this.$refs.PmsSkuStockList.getMain(this.selectedRowKeys[0])
      this.$refs.PmsProductPicsList.getMain(this.selectedRowKeys[0], this.selectionRows[0].productSn)
      this.$refs.PmsProductQualificationPicsList.getMain(this.selectedRowKeys[0], this.selectionRows[0].productSn)
    },
    onClearSelected() {
      this.selectedRowKeys = []
      this.selectionRows = []
      this.$refs.PmsSkuStockList.queryParam.mainId = null
      this.$refs.PmsSkuStockList.loadData()
      this.$refs.PmsSkuStockList.selectedRowKeys = []
      this.$refs.PmsSkuStockList.selectionRows = []
      //this.$refs.PmsProductPicsList.queryParam.mainId = null;
      this.$refs.PmsProductPicsList.selectedRowKeys = []
      this.$refs.PmsProductPicsList.selectionRows = []
      this.$refs.PmsProductPicsList.loadData()
      this.$refs.PmsProductQualificationPicsList.selectedRowKeys = []
      this.$refs.PmsProductQualificationPicsList.selectionRows = []
      this.$refs.PmsProductQualificationPicsList.loadData()
    },
    handleExportModal() {
      let param = {...this.getQueryParams()};
      console.log("导出参数",param)
      this.$refs.productExportModal.show(param)
    },
    handleStatusDetail(record) {
      this.$refs.productStatusModal.show(record)
    },
    handlePicDetail(record) {
      this.$refs.productPicModal.show(record)
    },
    handleDelete: function(id) {
      let that = this
      deleteAction(that.url.delete, { id: id }).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
          that.loadData()
          this.$refs.PmsSkuStockList.loadData()
          this.$refs.PmsProductPicsList.loadData()
          this.$refs.PmsProductQualificationPicsList.loadData()
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    handleSyncWxMa: function(id) {
      let that = this
      let params = {}
      let formData = new FormData()
      formData.append('ids', id)
      postAction(this.url.wxmaSyncBatch, formData).then((res) => {
        if (res.success) {
          this.$message.success(res.message)
          this.$emit('ok')
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    handleGetWxMa: function(id) {
      let that = this
      getAction(this.url.wxmaGetProduct, { id: id }).then((res) => {
        if (res.success) {
          this.$message.success(res.message)
          this.$emit('ok')
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    searchQuery: function() {
      this.selectedRowKeys = []
      this.selectionRows = []
      this.$refs.PmsSkuStockList.queryParam.mainId = null
      this.$refs.PmsSkuStockList.loadData()
      this.$refs.PmsSkuStockList.selectedRowKeys = []
      this.$refs.PmsSkuStockList.selectionRows = []
      //this.$refs.PmsProductPicsList.queryParam.mainId = null;
      this.$refs.PmsProductPicsList.loadData()
      this.$refs.PmsProductPicsList.selectedRowKeys = []
      this.$refs.PmsProductPicsList.selectionRows = []
      this.$refs.PmsProductQualificationPicsList.loadData()
      this.$refs.PmsProductQualificationPicsList.selectedRowKeys = []
      this.$refs.PmsProductQualificationPicsList.selectionRows = []
      this.loadData(1)
    },
    handlePublishStatus(record) {
      if (record.publishStatus == '1') {
        record.publishStatus = '0'
      } else {
        record.publishStatus = '1'
      }
      let editObj = {}
      editObj.id = record.id
      editObj.publishStatus = record.publishStatus
      this.handleStatusEdit(editObj)
    },
    handlePublishStatus2(record){
      if (record.storeStatus == 1) {
        record.storeStatus = 0
      } else {
        record.storeStatus = 1
      }
      let editObj = {}
      editObj.id = record.id
      editObj.storeStatus = record.storeStatus
      this.handleStatusEdit(editObj)
    },
    handleStatusEdit(record) {
      const that = this
      that.confirmLoading = true
      console.log(record)
      httpAction(this.url.editProduct, record, 'put').then((res) => {
        if (res.success) {
          that.$message.success(res.message)
          that.$emit('ok')
        } else {
          that.$message.warning(res.message)
          if (record.publishStatus == '1') {
            record.publishStatus = '0'
          } else {
            record.publishStatus = '1'
          }
          if (record.storeStatus == '1') {
            record.storeStatus = '0'
          } else {
            record.storeStatus = '1'
          }
        }
      }).finally(() => {
        that.confirmLoading = false;
      })
    },
    handleProduct () {
      this.$refs.productModalForm.add();
      this.$refs.productModalForm.title = "商品库";
    },
    selectOK(data) {
      let ids = [];
      //params.productIdList = [];
      for (let a = 0; a < data.length; a++) {
        ids.push(data[a].id);
      }
      console.log(ids);
      postAction(this.url.addBatch, ids).then((res) => {
        if (res.success) {
          this.$message.success(res.message);
          this.loadData();
        } else {
          this.$message.warning(res.message);
        }
      })
    },
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
